/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * This class represents a Cargo from database
 * @author Roque Rueda
 */
public class Cargo {

    private static CaracolQueryGenerator caracolQuery = new CaracolQueryGenerator();
    
    private int id;
    private String cargo;
    private String descripcion;
    
    private static final String ID = "id";
    private static final String CARGO = "cargo";
    private static final String DESCRIPCION = "descripcion";
    private static final String TABLE = "cargo";
    
    /**
     * Get a single cargo store in the database by searching it's id
     * in the table
     * @param id identifier that is going to be search
     * @return cargo instance initialize with the value of the database
     * if it's found the given id otherwise returns null
     * @throws SQLException if some db exception happend during this operation
     * this is throw again
     * @throws Exception if some exception happend during the execution of this
     * operation this is throw
     */
    public static Cargo getCargo(int id) throws SQLException, Exception {
        Cargo c = null;
        String[] columns = new String[] { ID, CARGO, DESCRIPCION };
        
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, where, null, null, null, null);
        
        if (rawCargo != null && rawCargo.size() > 0) {
            List<Object> row = (List<Object>) rawCargo.get(0);
            c  = new Cargo();
            c.id = id;
            c.setCargo(row.get(1).toString());
            c.setDescripcion(row.get(2).toString());
        }
        
        return c;
    }
    
    /**
     * Get all the cargos that exist in the database
     * @return {}
     * @throws SQLException
     * @throws Exception 
     */
    public static List<Cargo> getAll() throws SQLException, Exception {
        ArrayList<Cargo> cargos = new ArrayList<Cargo>();
        String[] columns = new String[] { ID, CARGO, DESCRIPCION };
        List<Object> rawCargo = caracolQuery.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawCargo) {
            List<Object> row = (List<Object>) object;
            Cargo c = new Cargo();
            c.id = new Double(row.get(0).toString()).intValue();
            c.setCargo(row.get(1).toString());
            c.setDescripcion(row.get(2).toString());
            cargos.add(c);
        }
        
        return cargos;
    }
    
    /** 
     * Saves the values of this instance in the database
     * @return true if the operation affects more than 0 rows
     * @throws SQLException if some exception related with the database happend during the excetion 
     * of this operation
     * @throws Exception if some exception happend during the operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save
            return true;
        }
        Map<String,Object> m = new LinkedHashMap<String,Object>();
        m.put(CARGO, getCargo());
        m.put(DESCRIPCION, getDescripcion());
        
        int rowsAffected = caracolQuery.insert(TABLE, m);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = caracolQuery.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqcargo\');"));
        }
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }

    /**
     * Updates the database row with this instance values
     * @return true if rows affected is > 0
     * @throws SQLException is some db exception happen this is throw
     * @throws Exception is some exception happen this is throw
     */
    public boolean update() throws SQLException, Exception {
        int rowsAffected;
        Map<String, Object> values = new LinkedHashMap<String, Object>();
        values.put(CARGO, getCargo());
        values.put(DESCRIPCION, getDescripcion());
        
        Map<String,Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        
        rowsAffected = caracolQuery.update(TABLE, values, conditions);
        
        return rowsAffected > 0;
    }
    
    /** 
     * Delete the cargo from database this delete is made by searching in the database if this instance id exist
     * 
     * @return true if the operation affect more thant one row otherwise returns false
     * @throws SQLException if the database throws an exception during the execution of this operation
     * @throws Exception if something other than the database throws an exception this is throw
     * 
     */
    public boolean delete() throws SQLException, Exception {
        if (id <= 0) {
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = caracolQuery.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }
    
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the cargo
     */
    public String getCargo() {
        return cargo;
    }

    /**
     * @param cargo the cargo to set
     */
    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    /**
     * @return the descripcion
     */
    public String getDescripcion() {
        return descripcion;
    }

    /**
     * @param descripcion the descripcion to set
     */
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    

    
    
}
